package com.grm.util;

import javax.websocket.Session;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * desc:
 *
 * @author gaorimao
 * @since 2023-10-23-9:19
 */
public class TailLogUtil {
    //要查看的日志文件路径
    private static String logFilePath = "/home/projects/springboot_task/task.log";
    private static BufferedReader bufferedReader;
    public static void pushLog(Session session) {
        try {
            // 执行tail命令，获取输入流
            if (bufferedReader == null) {
                Process process = Runtime.getRuntime().exec("tail -f " + logFilePath);
                InputStream inputStream = process.getInputStream();
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            }
            // 通过WebSocket发送给实时日志给客户端
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                session.getBasicRemote().sendText(line + "<br />");
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}
